/* 
 *
 * Persistence4J - Simple library for data persistence using java
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package com.avdy.p4j.jdbc.dbms.connfactory.xa;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.XADataSource;

import org.apache.log4j.Logger;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.avdy.p4j.jdbc.dbms.DataSourceAttr;


/**
 * 
 * @author Avdhesh Yadav
 */
class AtomikosXAProviderimpl extends AbsXaProviderImpl implements XAProvider
{
	static Logger logger = Logger.getLogger(AtomikosXAProviderimpl.class.getName());
	/** */
	private AtomikosDataSourceBean mAtomikosDataSourceBean;


	/**
	 * 
	 * @param schema database
	 * @param dsAttr DataSourceAttr
	 * 
	 * @throws Exception
	 */
	public AtomikosXAProviderimpl(String database, DataSourceAttr dsAttr) throws Exception
	{
		super(database, dsAttr);
		initilizeDatasource();
	}
	
	
	/**
	 * 
	 * @param dsAttr DataSourceAttr
	 * 
	 * @throws Exception
	 */
	private void initilizeDatasource() throws Exception
	{
		logger.info("initilizeDataSource mDatabase:"+mDatabase + "dsAttr.getDbmsType():" + mDsAttr.getDbmsType());
		mAtomikosDataSourceBean = new AtomikosDataSourceBean();
		String resourceName = mDsAttr.getDbmsType() +"/" + mDatabase;
		mAtomikosDataSourceBean.setUniqueResourceName(resourceName);
		mAtomikosDataSourceBean.setXaDataSourceClassName(mDsAttr.getXaDataSourceClassName());
		
		Properties props = new Properties();
		props.setProperty ( "user" , mDsAttr.getUserName() );
		props.setProperty ( "password" , mDsAttr.getPassword() );
		props.setProperty ( "serverName" , mDsAttr.getDatabaseHost());
		props.setProperty ( "portNumber" , String.valueOf(mDsAttr.getPort()));
		props.setProperty ( "databaseName" , mDatabase);
		
		mAtomikosDataSourceBean.setXaProperties(props);
		mAtomikosDataSourceBean.setMinPoolSize(mDsAttr.getMinPoolSize());
		mAtomikosDataSourceBean.setMaxPoolSize(mDsAttr.getMaxPoolSize());
		mAtomikosDataSourceBean.setTestQuery(mTestQuery);
		mAtomikosDataSourceBean.setDefaultIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
		
		mUserTransaction = new UserTransactionImp();
		mUserTransaction.setTransactionTimeout(60);
	}	
	
	
	/**
	 * 
	 *  @return XADataSource
	 */
	public XADataSource getXaDataSource()throws SQLException
	{
		return mAtomikosDataSourceBean.getXaDataSource();
	}
	
	
	/**
	 * 
	 * @return Connection
	 */
	public Connection getConnection()throws SQLException
	{
		return mAtomikosDataSourceBean.getConnection();
	}
	
	
	/**
	 * 
	 */
	public void stop()
	{
		mAtomikosDataSourceBean.close();
	}
}